package com.lyc.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author 李大神仙
 * @Date 2024/2/27 20:26
 * @Version 1.0
 */
@Slf4j
@Configuration
public class XxlJobExecutorConfig {
    @Value("${xxl.job.admin.adminAddresses}")
    private String adminAddresses;

    @Value("${xxl.job.admin.addresses}")
    private String address;

    @Value("${xxl.job.admin.appname}")
    private String appname;

    @Value("${xxl.job.admin.port}")
    private int port;

    @Value("${xxl.job.admin.accessToken}")
    private String accessToken;

    @Value("${xxl.job.admin.logPath}")
    private String logPath;

    @Value("${xxl.job.admin.logRetentionDays}")
    private int logRetentionDays;

    @Autowired
    private InetUtils inetUtils;


    @Bean
    @ConditionalOnMissingBean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(this.adminAddresses);
        xxlJobSpringExecutor.setAppname(this.appname);
        xxlJobSpringExecutor.setAddress(this.address);
        String ip_ = this.inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
        xxlJobSpringExecutor.setIp(ip_);
        xxlJobSpringExecutor.setPort(this.port);
        xxlJobSpringExecutor.setAccessToken(this.accessToken);
        xxlJobSpringExecutor.setLogPath(this.logPath);
        xxlJobSpringExecutor.setLogRetentionDays(this.logRetentionDays);
        return xxlJobSpringExecutor;
    }
}
